<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Product extends CI_Controller {

    function __construct(){
        parent::__construct();
        $this->load->model('product_model');
        #$this->output->enable_profiler(true);
    }

    public function index(){
        $orderby  = $this->input->get('orderby');
        $orderway = $this->input->get('orderway');
        if ($orderway == 'desc'){
            $orderway = 'desc';
        } else {
            $orderway = 'asc';
        }

        if ($orderby == 'price'){
            $orderby = 'price_old';
        } else if ($orderby == 'name'){
            $orderby = 'title';
        } else if ($orderby == 'quantity'){
        } else {
            $orderby = 'price_old';
        }
        $data['orderby'] = $orderby;
        $data['orderway'] = $orderway;
        $data['result'] = $this->product_model->get_product(0, 0, 0, 0, $orderby, $orderway);
        $data['tpl'] = 'category';

        $data['meta_title']    = "Sản phẩm - ".$this->config->item('website_name'). ' | '.$this->config->item('website_domain');
        $data['meta_keyword']  = $this->config->item('website_keyword');
        $data['meta_desc']     = $this->config->item('website_desc');
        $this->load->view('_layout/index', $data);
    }

    function detail($product_id = 0){
        $product_id = intval($product_id);
        if ($product_id != 0){
            $this->db->set('viewed', 'viewed+1', false);
            $this->db->where('product_id', $product_id);
            $this->db->update(TBL_PRODUCT);

            $data['images']            = $this->product_model->get_images($product_id);
            $data['data']              = $this->product_model->get_product($product_id);

            if (count($data['data']) == 0){
                redirect('san-pham');
            }
            $data['category_info']     = $this->product_model->get_category($data['data']['category_id']);

            $product_relative          = $this->product_model->get_product(0, $data['data']['category_id'], 6);
            foreach($product_relative as $key => $value){
                if ($value['product_id'] == $data['data']['product_id']){
                    unset($product_relative[$key]);
                }
            }
            $data['product_relative']  = $product_relative;

            $data['meta_title']    = $data['data']['title']." - ".$this->config->item('website_name'). ' | '.$this->config->item('website_domain');
            $data['meta_keyword']  = $this->config->item('website_keyword');
            $data['meta_desc']     = $this->config->item('website_desc');

            $data['tpl']               = 'product/detail';
            $this->load->view('_layout/index', $data);
        } else {
            redirect('product');
        }
    }

    function category($category_id = 0){
        $category_id = intval($category_id);

        if ($category_id != 0){
            /* Check exist */
            $exist = $this->product_model->get_category($category_id);
            if (empty($exist)){
                redirect('san-pham');
            }

            /* Order condition */
            $orderby  = trim($this->input->get('orderby'));
            $orderway = trim($this->input->get('orderway')) == 'desc' ? 'desc' : 'asc';
            if ($orderby == 'price'){
                $orderby = 'price_old';
            } else if ($orderby == 'name'){
                $orderby = 'title';
            } else if ($orderby == 'quantity'){
            } else {
                $orderby = 'price_old';
            }
            $data['orderby'] = $orderby;
            $data['orderway'] = $orderway;

            /* Get all child category */
            $child_category = array();
            $categories     = array();
            $child_category_data   = $this->product_model->get_child_category($category_id, true);
            foreach($child_category_data as $key => $value){
                $child_category[]   = $value['category_id'];
                $categories[$value['category_id']] = $value['title'];
            }
            $child_category = array_unique($child_category);
            $child_category[0] = $category_id;

            if (count($child_category) > 0){
                $data['result']        = $this->product_model->get_product_by_category($child_category);
            }

            $category_info         = $this->product_model->get_category($category_id);
            $data['categories']    = $categories;
            $data['category_info'] = $category_info;

            $data['meta_title']    = $category_info['title']." - ".$this->config->item('website_name'). ' | '.$this->config->item('website_domain');
            $data['meta_keyword']  = $this->config->item('website_keyword');
            $data['meta_desc']     = $this->config->item('website_desc');

            $data['url']           = base_url('san-pham/'.$category_info['category_id'].'_'.url_friendly($category_info['title']));
            $data['tpl']           = 'product/category';
            $this->load->view('_layout/index', $data);
        } else {
            redirect('product');
        }
    }

    function compare(){
        if ($_POST){
            $compare_array = explode(",", trim($this->input->post('compare_product_list'), ','));
            $compare = array();
            foreach($compare_array as $key => $value){
                $item = intval(only_numeric($value));
                if (!in_array($item, $compare)){
                    $compare[] = $item;
                }
            }

            if (count($compare) == 2){
                $_SESSION['compare'] = $compare;
            } else {
                redirect('san-pham');
            }
        }

        if (isset($_SESSION['compare'])){
            $current = $_SESSION['compare'];
            foreach($current as $key => $value){
                $products[] = $this->product_model->get_product($value);
            }
            $data['products'] = $products;
        } else {
            unset($_SESSION['compare']);
            redirect('san-pham');
        }

        $data['tpl'] = 'product/compare';
        $this->load->view('_layout/index', $data);
    }

    function search(){
        $keyword = $this->input->get('keyword');
        $result  = $this->product_model->search($keyword, array('active_flag' => ACTIVE_FLAG, 'delete_flag' => UNDELETE_FLAG));
        $data['result'] = $result;
        $data['tpl'] = 'category';
        $this->load->view('_layout/index', $data);
    }

    function product_by_manufacturer($manufacturer_id = 0){
        $manufacturer_id = intval($manufacturer_id);
        $manufacturer_info = $this->product_model->get_manufacturer($manufacturer_id);

        $data['meta_title']    = "Thương hiệu ".$manufacturer_info['title']." - ".$this->config->item('website_name'). ' | '.$this->config->item('website_domain');
        $data['meta_keyword']  = $this->config->item('website_keyword');
        $data['meta_desc']     = $this->config->item('website_desc');

        $data['result'] = $this->product_model->get_product_by_manufacturer($manufacturer_id);
        $data['tpl'] = 'product/category';
        $this->load->view('_layout/index', $data);
    }

    function product_relative($category_id = 0, $limit = 6){
        $category_id = intval($category_id);
        $limit = intval($limit);
        $data['product_relative'] = $this->product_model->get_product(0, $category_id, $limit);
        $data['tpl'] = 'product/product_relative';
        $this->load->view('_layout/single', $data);
    }

    function block_category(){
        $data['categories'] = $this->product_model->get_category();
        $data['tpl'] = 'product/block_category';
        $this->load->view('_layout/single', $data);
    }

    function block_hot(){
        $data['product_hot'] = $this->product_model->get_product_hot(0, 6);
        $data['tpl'] = 'product/block_hot';
        $this->load->view('_layout/single', $data);
    }

    function block_promotion(){
        $data['product_promotion'] = $this->product_model->get_product_promotion(6);
        $data['tpl'] = 'product/block_promotion';
        $this->load->view('_layout/single', $data);
    }

    function block_newest(){
        $data['product_newest'] = $this->product_model->get_product(0, 0, 6, 0, 'create_date', 'DESC');
        $data['tpl'] = 'product/block_hot';
        $this->load->view('_layout/single', $data);
    }

    function manufacturer(){
        $data['result'] = $this->product_model->get_manufacturer();
        $data['tpl'] = 'product/manufacturer';
        $this->load->view('_layout/index', $data);
    }

    function ads(){
        $type        = trim($this->input->get('type'));
        $list        = trim($this->input->get('view')) == 'list' ? true : false;
        $number      = intval($this->input->get('num'));
        $return      = trim($this->input->get('result'));
        $number      = $number != 0 ? $number : 5;
        $this->db->select('*')->from(TBL_PRODUCT);
        $this->db->where('delete_flag', UNDELETE_FLAG);
        $this->db->where('active_flag', ACTIVE_FLAG);
        $this->db->limit($number);

        //Type
        if ($type == 'hot'){
            $this->db->where('hot_flag', 1);
        }
        elseif ($type == 'newest'){
            $this->db->order_by('create_date DESC');
        }

        //Order
        $this->db->order_by('Rand()');

        //Result
        $result     = $this->db->get()->result_array();

        if ($return == 'json'){
            die(json_encode($result));
        } elseif ($return == 'xml'){
            include_once(APPPATH.'libraries/array2xml.php');
            $this->output->set_content_type('text/xml');
            $xml = new arr2xml($result);
            echo $xml->get_xml();
            die();
        }

        $data['tpl'] = 'product/ads';
        $data['result'] = $result;
        $this->load->view('_layout/single', $data);
    }
}